package momo.app.status;

import momo.app.comms.CommsManager;
import momo.app.structure.Message;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


public class ElectionTimeout extends Thread
{
	static final long DEFAULT_COUNTDOWN_TIME = 10 * 1000;
	private Log log = LogFactory.getLog(ElectionTimeout.class);
	private long countdownTime;
	
	public ElectionTimeout()
	{
		countdownTime = DEFAULT_COUNTDOWN_TIME;
	}
	
	public void run()
	{
		if ( log.isDebugEnabled() )
			log.debug("Event: Countdown event has just started");
		try
		{
			sleep(countdownTime);
			if ( log.isDebugEnabled() )
				log.debug("Has reach the time for General Election");
			
			CommsManager.getInstance().startElection(Message.GENERAL_ELECTION);
			
		}catch (InterruptedException ie)
		{
//			log.error("InterruptedException", ie);
		}catch (Exception e)
		{
			if ( log.isErrorEnabled() )
				log.error("Exception", e);
		}
		if ( log.isDebugEnabled() )
			log.debug("Event: Countdown event has just end");
	}
	
	public void stopEvent()
	{
		if ( this.isAlive() )
			this.interrupt();
	}
	
}//end of class ElectionTimeout_2
